<?php

class AttributeController extends Controller
{
	public function actionIndex()
	{
        $this->pageTitle = Yii::t('app', 'Manager attribute Step Analyse and Matter');

        $model = new Attribute();

        if(isset($_GET['Attribute'])) {
            $model->unsetAttributes();
            $model->attributes=$_GET['Attribute'];
        }

		$this->render('index', array(
            'model' => $model,
            'matterList' => Matter::getListMatter(),
            'analysisList' => Analysis::getListAnalysis(),

        ));
	}

    public function actionAdd(){
        $this->pageTitle = Yii::t('app', 'Manager attribute Step Analyse and Matter');

        $model = new Attribute();

        if (isset($_POST['Attribute']) && $data = $_POST['Attribute']) {
            $error = array();
            $model->attributes = $data;
            if(!empty($data['id_analysis'])){
                $model->id_analysis = implode(',',$data['id_analysis']);
            }else{
                $error[] = Yii::t('app', 'Please check analysis!');
            }

            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Attribute created success!'));
                $this->redirect(array('attribute/index'));
            }else{
                if(empty($error))
                    $error[] = Yii::t('app', 'Attribute create false!');
            }
            if($error){
                Yii::app()->user->setFlash('error', implode('<br>', $error));
            }
        }

        $this->render('add', array(
            'model' => $model,
            'matterList' => Matter::getListMatter(),
            'analysisList' => Analysis::getListAnalysis(),

        ));
    }

    public function actionUpdate($id = null) {
        $this->pageTitle = Yii::t('app', 'Manager attribute Step Analyse and Matter');
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(array('attribute/index'));
        }

        //Get Attribute type
        $model = Attribute::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(array('attribute/index'));
        }

        if (isset($_POST['Attribute']) && $data = $_POST['Attribute']) {
            $error = array();
            $model->attributes = $data;
            if(!empty($data['id_analysis'])){
                $model->id_analysis = implode(',',$data['id_analysis']);
            }else{
                $error[] = Yii::t('app', 'Please check analysis!');
            }

            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Attribute created success!'));
                $this->redirect(array('attribute/index'));
            }else{
                if(empty($error))
                    $error[] = Yii::t('app', 'Attribute create false!');
            }
            if($error){
                Yii::app()->user->setFlash('error', implode('<br>', $error));
            }
        }

        // for update attribute
        if($model->id_analysis){
            $model->id_analysis = explode(',', $model->id_analysis);
        }
        $this->render('update', array(
            'model' => $model,
            'matterList' => Matter::getListMatter(),
            'analysisList' => Analysis::getListAnalysis(),
        ));
    }

    public function actionDelete($id = null) {
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $model = Attribute::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $model->del_flg = 1;
        if ($model->save(false)) {
            if(!isset($_GET['ajax'])){
                Yii::app()->user->setFlash('success', Yii::t('app', 'Attribute - Deleted Successfully'));
                $this->redirect(array('attribute/index'));
            }
            else{
                echo '<div class="alert alert-success fade in"><button type="button" class="close" data-dismiss="alert">×</button>'.Yii::t('app', 'Attribute - Deleted Successfully').'</div>';
                die();
            }

        }

    }

}